package org.dtrd.modules.tool.service.impl;

import org.dtrd.modules.tool.entity.po.DtrdEntRdTools;
import org.dtrd.modules.tool.mapper.DtrdEntRdToolsMapper;
import org.dtrd.modules.tool.service.IDtrdEntRdToolsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.dtrd.modules.tool.entity.bean.ToolsDetail;
import org.dtrd.modules.tool.entity.bean.ToolsListInfo;
import org.dtrd.modules.tool.entity.bean.ToolsListRequest;
import org.dtrd.modules.tool.entity.bean.ToolsPageRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.*;
import java.util.stream.Collectors;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiao
 * @since 2022-05-25
 */
@DS("multi-datasource1")
@Service
public class DtrdEntRdToolsServiceImpl extends ServiceImpl<DtrdEntRdToolsMapper, DtrdEntRdTools> implements IDtrdEntRdToolsService {

    @Override
    public List<ToolsListInfo> getToolsList() {
        LambdaQueryWrapper<DtrdEntRdTools> queryWrapper = baseQueryWrapper();
        List<DtrdEntRdTools> poList = list(queryWrapper);
        List<ToolsListInfo> dtoList = new ArrayList<>(poList.size());
        List<ToolsListInfo> dataList  = new ArrayList<>(poList.size() / 2);

        if (!poList.isEmpty()) {
            Map<String, List<ToolsListInfo>> tmpMap2 = new HashMap(dtoList.size() / 2);
            poList.forEach(p -> {
                List<ToolsListInfo> tmpList = tmpMap2.get(p.getCategoryName());
                if (tmpList == null) {
                    tmpList = new ArrayList<>();
                }
                ToolsListInfo info = new ToolsListInfo(p);
                tmpList.add(info);
                tmpMap2.put(p.getCategoryName(), tmpList);
            });

            tmpMap2.keySet().forEach(key -> {
                ToolsListInfo tools = new ToolsListInfo();
                tools.setCategoryName(key);
                tools.setList(tmpMap2.get(key));
                dataList.add(tools);
            });
        }
        return dataList;
    }

    @Override
    public IPage<ToolsListInfo> pageTools(ToolsPageRequest request) {
        LambdaQueryWrapper<DtrdEntRdTools> queryWrapper = baseQueryWrapper();
        IPage<DtrdEntRdTools> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = this.page(page, queryWrapper);
        return page.convert(ToolsListInfo::new);
    }

    @Override
    public ToolsDetail getTools(Integer dataId) {
        DtrdEntRdTools dtrdEntRdTools = getById(dataId);
        return Optional.ofNullable(dtrdEntRdTools)
                        .map(po -> new ToolsDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateTools(ToolsDetail dto) {
        DtrdEntRdTools dtrdEntRdTools = new DtrdEntRdTools();
        if(dto.getDataId() == null){
            dtrdEntRdTools = dtrdEntRdTools.init();
        }
        dtrdEntRdTools.parseFromDto(dto);
        return saveOrUpdate(dtrdEntRdTools);
    }

    @Override
    public boolean removeTools(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdEntRdTools> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntRdTools.class)
                        .eq(DtrdEntRdTools::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntRdTools> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntRdTools.class)
                        .eq(DtrdEntRdTools::getIsDel, 0);
    }
}
